package com.yourbatis.syntax;

import com.yourbatis.interpreter.Environment;

import java.util.List;

public class BlockStatement implements Statement {
    private final List<Statement> statements;

    public BlockStatement(List<Statement> statements) {
        this.statements = statements;
    }

    @Override
    public Object execute() {
        Environment.enterScope();
        Object val = null;
        try {
            for (Statement stmt : statements) {
                val = stmt.execute();
            }
        } catch (RuntimeException e) {
            // 捕获return异常，正常退出作用域
            Environment.exitScope();
            throw e; // 继续向上传播
        }
        Environment.exitScope();
        return val;
    }


}
